from pymongo import MongoClient
import csv
client = MongoClient(port=27017)
db=client['tiktok']
with open('E:\\data_pi\\hashtag_pct.tsv','w', encoding='utf-8',newline='\n') as fout:
    writer = csv.writer(fout, delimiter='\t')
    writer.writerow(['Hashtag','Nums of videos','Label A =0','PCT A','Label B =0','PCT B','Both labels =0','PCT AB'])
    for col in db.list_collection_names():
         print(col)
         t=0
         i=0
         j=0
         k=0
         for obj in db[col].find():
             if len(obj['video_feature']['img_embed']) > 0 and len(obj['text_feature']['text'])>0 and len(obj['video_feature']['audio']['yamnet'])>0 and(len(obj['video_feature']['label'])>0)and (len(obj['video_feature']['residual'])>0):
                 if 'labelA_n' in obj['video_feature']['label'].keys():
                    t+=1
                    if int(obj['video_feature']['label']['labelA_n']) == 1:
                        i += 1
                    if int(obj['video_feature']['label']['labelB_n']) == 1:
                        j+=1
                    if int(obj['video_feature']['label']['labelA_n']) == 1 and int(obj['video_feature']['label']['labelB_n']) == 1 :
                        k+=1
         if t==0:
             print('na')
             writer.writerow([col,'NA'])
             continue
         print('Label A =0: ',i,t,i/t)
         print('Label B =0: ',j,t,j/t)
         print('Label A =1 and Label B=1: ',k,t,k/t)
         writer.writerow([col, t,i,i/t,j,j/t,k,k/t])
